1
|
|
|
/** |
2
|
|
|
* Inji js core |
3
|
|
|
* |
4
|
|
|
* @author Alexey Krupskiy <[email protected]> |
5
|
|
|
* @link http://inji.ru/ |
6
|
|
|
* @copyright 2015 Alexey Krupskiy |
7
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
function Inji() { |
11
|
|
|
this.options = {}; |
12
|
|
|
this.onLoadCallbacks = []; |
13
|
|
|
this.loaded = false; |
14
|
|
|
this.listeners = {}; |
15
|
|
|
this.loadedScripts = {}; |
16
|
|
|
} |
17
|
|
|
Inji.prototype.onLoad = function (callback, start) { |
18
|
|
|
if (typeof callback == 'function') { |
19
|
|
|
if (this.loaded) { |
20
|
|
|
callback(); |
21
|
|
|
} else { |
22
|
|
|
if (start) { |
23
|
|
|
this.onLoadCallbacks.unshift(callback); |
24
|
|
|
} else { |
25
|
|
|
this.onLoadCallbacks.push(callback); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
Inji.prototype.startCallbacks = function () { |
31
|
|
|
console.log('inji start onload'); |
|
|
|
|
32
|
|
|
while (callback = this.onLoadCallbacks.shift()) { |
|
|
|
|
33
|
|
|
if (typeof callback == 'function') { |
34
|
|
|
callback(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
if (this.onLoadCallbacks.length != 0) { |
38
|
|
|
this.startCallbacks(); |
39
|
|
|
} |
40
|
|
|
var indicator = document.getElementById('loading-indicator'); |
41
|
|
|
if (indicator) { |
42
|
|
|
indicator.style.display = 'none'; |
43
|
|
|
} |
44
|
|
|
inji.loaded = true; |
45
|
|
|
console.log('inji start complete'); |
46
|
|
|
} |
47
|
|
|
Inji.prototype.start = function (options) { |
48
|
|
|
console.log('Inji start'); |
|
|
|
|
49
|
|
|
for (key in options.compresedScripts) { |
|
|
|
|
50
|
|
|
inji.loadedScripts[options.compresedScripts[key]] = true; |
51
|
|
|
} |
52
|
|
|
this.options = options; |
53
|
|
|
if (options.onLoadModules) { |
54
|
|
|
this.onLoad(function () { |
55
|
|
|
for (key in options.onLoadModules) { |
|
|
|
|
56
|
|
|
if (typeof inji[key] == 'undefined') { |
57
|
|
|
inji[key] = new window[key](); |
58
|
|
|
if (typeof (inji[key].init) == 'function') { |
59
|
|
|
console.log(key + ' init'); |
|
|
|
|
60
|
|
|
inji[key].init(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
}, true) |
65
|
|
|
} |
66
|
|
|
if (options.scripts.length > 0) { |
67
|
|
|
this.loadScripts(options.scripts, 0); |
68
|
|
|
} else { |
69
|
|
|
inji.startCallbacks(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
Inji.prototype.loadScripts = function (scripts, key) { |
73
|
|
|
this.addScript(scripts[key], function () { |
74
|
|
|
if (typeof (scripts[key].name) != 'undefined') { |
75
|
|
|
inji.loadedScripts[scripts[key].file] = true; |
76
|
|
|
if (typeof inji[scripts[key].name] == 'undefined') { |
77
|
|
|
console.log('js ' + scripts[key].name + '(' + scripts[key].file + ') loaded'); |
|
|
|
|
78
|
|
|
inji[scripts[key].name] = new window[scripts[key].name](); |
79
|
|
|
if (typeof (inji[scripts[key].name].init) == 'function') { |
80
|
|
|
inji[scripts[key].name].init(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} else if (typeof (scripts[key].file) != 'undefined') { |
84
|
|
|
inji.loadedScripts[scripts[key].file] = true; |
85
|
|
|
console.log('js ' + scripts[key].file + ' loaded'); |
86
|
|
|
|
87
|
|
|
} else { |
88
|
|
|
inji.loadedScripts[scripts[key]] = true; |
89
|
|
|
console.log('js ' + scripts[key] + ' loaded'); |
90
|
|
|
inji.event('loadScript', scripts[key]); |
91
|
|
|
} |
92
|
|
|
if (typeof (scripts[key + 1]) != 'undefined') { |
93
|
|
|
inji.loadScripts(scripts, key + 1); |
94
|
|
|
} else { |
95
|
|
|
console.log('All scripts loaded'); |
96
|
|
|
inji.startCallbacks(); |
97
|
|
|
} |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
Inji.prototype.addScript = function (script, callback) { |
101
|
|
|
var element = document.createElement('script'); |
102
|
|
|
var src = ''; |
103
|
|
|
if (typeof (script.file) != 'undefined') { |
104
|
|
|
src = script.file; |
105
|
|
|
} else { |
106
|
|
|
src = script; |
107
|
|
|
} |
108
|
|
|
if (inji.loadedScripts[src]) { |
109
|
|
|
if (typeof (callback) == 'function') { |
110
|
|
|
callback(); |
111
|
|
|
} |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
element.src = src; |
115
|
|
|
element.type = 'text/javascript'; |
116
|
|
|
if (typeof (callback) == 'function') { |
117
|
|
|
element.onload = callback; |
118
|
|
|
} |
119
|
|
|
document.head.appendChild(element); |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
Inji.prototype.on = function (eventType, callback) { |
124
|
|
|
if (typeof this.listeners[eventType] == 'undefined') { |
125
|
|
|
this.listeners[eventType] = []; |
126
|
|
|
} |
127
|
|
|
this.listeners[eventType].push(callback); |
128
|
|
|
} |
129
|
|
|
Inji.prototype.event = function (eventType, object) { |
130
|
|
|
if (typeof this.listeners[eventType] != 'undefined') { |
131
|
|
|
for (key in this.listeners[eventType]) { |
|
|
|
|
132
|
|
|
this.listeners[eventType][key](eventType, object); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
Inji.prototype.randomString = function (length) { |
137
|
|
|
if (!length) { |
138
|
|
|
length = 20; |
139
|
|
|
} |
140
|
|
|
var text = ""; |
141
|
|
|
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
142
|
|
|
|
143
|
|
|
for (var i = 0; i < length; i++) |
144
|
|
|
text += chars.charAt(Math.floor(Math.random() * chars.length)); |
145
|
|
|
|
146
|
|
|
return text; |
147
|
|
|
} |
148
|
|
|
Inji.prototype.numberFormat = function (number, decimals, dec_point, thousands_sep) { |
149
|
|
|
//// Format a number with grouped thousands |
150
|
|
|
// |
151
|
|
|
// + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) |
152
|
|
|
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
153
|
|
|
// + bugfix by: Michael White (http://crestidg.com) |
154
|
|
|
|
155
|
|
|
var i, j, kw, kd, km; |
156
|
|
|
|
157
|
|
|
// input sanitation & defaults |
158
|
|
|
if (isNaN(decimals = Math.abs(decimals))) { |
159
|
|
|
decimals = 2; |
160
|
|
|
} |
161
|
|
|
if (dec_point == undefined) { |
162
|
|
|
dec_point = ","; |
163
|
|
|
} |
164
|
|
|
if (thousands_sep == undefined) { |
165
|
|
|
thousands_sep = "."; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
i = parseInt(number = (+number || 0).toFixed(decimals)) + ""; |
169
|
|
|
|
170
|
|
|
if ((j = i.length) > 3) { |
171
|
|
|
j = j % 3; |
172
|
|
|
} else { |
173
|
|
|
j = 0; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
km = (j ? i.substr(0, j) + thousands_sep : ""); |
177
|
|
|
kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep); |
178
|
|
|
//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : ""); |
179
|
|
|
kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : ""); |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
return km + kw + kd; |
183
|
|
|
} |
184
|
|
|
Inji.prototype.get = function (query) { |
185
|
|
|
var element = document.querySelector(query); |
186
|
|
|
if (element) { |
187
|
|
|
return new function () { |
188
|
|
|
this.element = element; |
189
|
|
|
this.attr = function (name) { |
190
|
|
|
for (var key in this.element.attributes) { |
|
|
|
|
191
|
|
|
var attr = element.attributes[key]; |
192
|
|
|
if (attr.name == name) { |
193
|
|
|
return attr.value; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
return null; |
197
|
|
|
} |
198
|
|
|
this.data = function (name) { |
199
|
|
|
var data = this.attr('data-' + name); |
200
|
|
|
try { |
201
|
|
|
return JSON.parse(data); |
202
|
|
|
} catch (e) { |
203
|
|
|
return data; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
var inji = new Inji(); |